package com.hiof.location;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import com.hiof.Configuration;
import com.hiof.data.MoraService;
import com.hiof.servercommunication.receive.beans.GeoPoint;
import com.hiof.settings.user.UserConfiguration;
import com.hiof.utility.Utility;

/**
 * Created with IntelliJ IDEA.
 * User: manish
 * Date: 4/17/12
 * Time: 6:50 PM
 * To change this template use File | Settings | File Templates.
 */
public class GoogleGpsManager extends GPSManager implements LocationListener {
    private static GoogleGpsManager instance;

    public static GoogleGpsManager getInstance() {
        if (null == instance) {
            instance = new GoogleGpsManager();
        }
        return instance;
    }

    private GoogleGpsManager() {
        initLiveLocationManager();
    }

    @Override
    public GeoPoint getLatestLocation() {
        MoraService moraService = MoraService.getInstance();
        GeoPoint latestLocation = moraService.getSelfLocationInfo();
        return latestLocation;
    }

    @Override
    public double getLatestSpeed() {
        MoraService moraService = MoraService.getInstance();
        return moraService.getSelfSpeed();
    }

    public void onLocationChanged(Location location) {
        if (UserConfiguration.getInstance() == null || UserConfiguration.getInstance().getUserId() == 0)
            return;
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        double speed = location.getSpeed();
        double altitude = location.getAltitude();

        MoraService moraService = MoraService.getInstance();
        moraService.saveSelfLocationInfo(UserConfiguration.getInstance().getUserId(), latitude,
                longitude, altitude, speed, UserConfiguration.getInstance().getTypeOfAttendant());

        GeoPoint geoPoint = new GeoPoint(latitude, longitude, altitude);
        fireEvent(geoPoint);
    }

    public void onStatusChanged(String s, int i, Bundle bundle) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void onProviderEnabled(String s) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void onProviderDisabled(String s) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void destroy() {
        if (instance != null) {
            instance = null;
        }
    }

    private void initLiveLocationManager() {
        LocationManager locationManager = (LocationManager) Configuration.getInstance().getCurrentContext()
                .getSystemService(Context.LOCATION_SERVICE);
        String provider = LocationManager.GPS_PROVIDER;
        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Utility.debug(this.getClass().getName(), "provider enabled");
            provider = LocationManager.NETWORK_PROVIDER;
            createGpsDisabledAlert();
        }
        Utility.debug(this.getClass().getName(), "current provider:"+provider);
        locationManager.requestLocationUpdates(provider, 0, 0, this);
    }

    private void createGpsDisabledAlert(){
        AlertDialog.Builder builder = new AlertDialog.Builder(Configuration.getInstance().getCurrentContext());
        builder.setMessage("Your GPS is disabled! Would you like to enable it?")
                .setCancelable(false)
                .setPositiveButton("Enable GPS",
                        new DialogInterface.OnClickListener(){
                            public void onClick(DialogInterface dialog, int id){
                                showGpsOptions();
                            }
                        });
        builder.setNegativeButton("Do nothing",
                new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int id){
                        dialog.cancel();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }

    private void showGpsOptions(){
        Intent gpsOptionsIntent = new Intent(
                android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        Configuration.getInstance().getCurrentContext().startActivity(gpsOptionsIntent);
    }
}
